package com.rk.common.config;
import com.rk.common.constant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :Rk.
 * @date : 2022/12/6
 */
@Configuration
public class RabbitMQConfig {

    /*==========================================订单处理交换机================================================*/

    /**
     * 定义订单交换机
     * @return
     */
    @Bean
    public DirectExchange directOrderExchange(){
        return new DirectExchange(MqConst.EXCHANGE_DIRECT_ORDER);
    }


    /**
     * 定义订单队列
     * @return
     */
    @Bean
    public Queue OrderQueue(){
        return new Queue(MqConst.QUEUE_ORDER);
    }

    /**
     * 绑定订单队列 到 订单交换机
     * @param OrderQueue
     * @param directOrderExchange
     * @return
     */
    @Bean
    public Binding bindingOrder(Queue OrderQueue,DirectExchange directOrderExchange){
        return BindingBuilder.bind(OrderQueue).to(directOrderExchange).with(MqConst.ROUTING_ORDER);
    }

    /*==========================================消息发送交换机================================================*/


    /**
     * 定义消息发送交换机
     * @return
     */
    @Bean
    public DirectExchange directMessageExchange(){
        return new DirectExchange(MqConst.EXCHANGE_DIRECT_MESSSAGE);
    }


    /**
     * 定义短信发送队列
     * @return
     */
    @Bean
    public Queue MSMQueue(){
        return new Queue(MqConst.QUEUE_MSM);
    }


    /**
     * 绑定短信发送队列 到 消息发送交换机
     * @param MSMQueue
     * @param directMessageExchange
     * @return
     */
    @Bean
    public Binding bindingMsmtoMessage(Queue MSMQueue,DirectExchange directMessageExchange){
        return BindingBuilder.bind(MSMQueue).to(directMessageExchange).with(MqConst.ROUTING_MSM);
    }


    /*==========================================绑定邮件队列 到 消息发送交换机================================================*/

    /**
     * 定义邮件发送队列
     * @return
     */
    @Bean
    public Queue Emailqueue(){
        // 邮件发送队列绑定我们的死信交换机
        Map<String, Object> arguments = new HashMap<>(2);
        //要绑定的死信交换机
        arguments.put("x-dead-letter-exchange", MqConst.EXCHANGE_DIRECT_DLX);
        //要绑定的死信队列
        arguments.put("x-dead-letter-routing-key", MqConst.ROUTING_DLX);
        return new Queue(MqConst.QUEUE_EMAIL,true,false , false,arguments);
    }


    /**
     * 绑定邮件队列 到 消息发送交换机
     * @param Emailqueue
     * @param directMessageExchange
     * @return
     */
    @Bean
    public Binding bindingEmailtoMessage(Queue Emailqueue,DirectExchange directMessageExchange){
        return BindingBuilder.bind(Emailqueue).to(directMessageExchange).with(MqConst.ROUTING_EMAIL);
    }


    /*==========================================死信交换机================================================*/

    /**
     * 定义死信交换机
     * @return
     */
    @Bean
    public DirectExchange directDlxExchange(){
        return new DirectExchange(MqConst.EXCHANGE_DIRECT_DLX);
    }


    /**
     * 定义死信队列
     * @return
     */
    @Bean
    public Queue Dlxqueue(){
        return new Queue(MqConst.QUEUE_DLX);
    }

    /**
     * 绑定死信队列 到 死信交换机
     */
    @Bean
    public Binding bindingDlxtoDlxExchange(Queue Dlxqueue,DirectExchange directDlxExchange){
        return BindingBuilder.bind(Dlxqueue).to(directDlxExchange).with(MqConst.ROUTING_DLX);
    }



    /*==========================================定时任务交换机================================================*/

    /**
     * 定义定时任务交换机
     * @return
     */
    @Bean
    public DirectExchange directTaskExchange(){
        return new DirectExchange(MqConst.EXCHANGE_DIRECT_TASK);
    }


    /**
     * 定义定时任务队列---预约提醒
     * @return
     */
    @Bean
    public Queue Taskqueue(){
        return new Queue(MqConst.QUEUE_TASK_8);
    }

    /**
     * 绑定定时任务队列 到 定时任务交换机
     */
    @Bean
    public Binding bindingTasktTaskExchange(Queue Taskqueue,DirectExchange directTaskExchange){
        return BindingBuilder.bind(Taskqueue).to(directTaskExchange).with(MqConst.ROUTING_TASK_8);
    }


    /*==========================================配置消息转换器  将传递的对象转换为json================================================*/

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

}
